package com.example.Gui;

import com.example.AdminComponent.*;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


//管理员界面
public class AdminUi {
      JFrame jFrame=new JFrame("欢迎进入管理员界面");
      private String user;
      private String passWd;


      void init(String user,String passWd){
          this.user=user;
          this.passWd=passWd;
          jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          jFrame.setVisible(true);
          jFrame.setSize(900,500);
          jFrame.setLocationRelativeTo(null);
          jFrame.setResizable(false);
          jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

          //设置菜单栏
          JMenuBar jmb=new JMenuBar();  //菜单条
          JMenu jMenu=new JMenu("设置");
          JMenuItem m1=new JMenuItem("切换账号");
          JMenuItem m2=new JMenuItem("退出程序");
          JMenuItem m3=new JMenuItem("修改密码");
          //点击切换账号
          m1.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                  try {
                      new LoginUI().init();
                  } catch (Exception ex) {
                      throw new RuntimeException(ex);
                  }
                  jFrame.dispose();
              }
          });

          //点击退出程序
          m2.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                  System.exit(0);
              }
          });
          //点击修改密码
          m3.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                  new PassJFrame(user,passWd);
              }
          });
          jMenu.add(m1);
          jMenu.add(m2);
          jMenu.add(m3);
          jmb.add(jMenu);
          jFrame.setJMenuBar(jmb);

          //设置分割面板
          JSplitPane jSplitPane=new JSplitPane();
          //支持连续布局
          jSplitPane.setContinuousLayout(true);
          jSplitPane.setDividerLocation(150);
          jSplitPane.setDividerSize(5);
          //设置左侧内容
          DefaultMutableTreeNode root = new DefaultMutableTreeNode("系统管理");
          DefaultMutableTreeNode student = new DefaultMutableTreeNode("学生管理");
          DefaultMutableTreeNode teacher = new DefaultMutableTreeNode("教师管理");
          DefaultMutableTreeNode aaClass = new DefaultMutableTreeNode("课程管理");
          DefaultMutableTreeNode course = new DefaultMutableTreeNode("课程申请");
          root.add(student);
          root.add(teacher);
          root.add(aaClass);
          root.add(course);
          JTree tree=new JTree(root);
          jSplitPane.setLeftComponent(tree);
          //监听student
          tree.addTreeSelectionListener(new TreeSelectionListener() {
              @Override
              public void valueChanged(TreeSelectionEvent e) {
                  Object lastPathComponent = e.getNewLeadSelectionPath().getLastPathComponent();
                  if(lastPathComponent.equals(student)){
                      //默认左右宽度
                      jSplitPane.setDividerLocation(150);
                      jSplitPane.setRightComponent(new Student());

                  } else if (lastPathComponent.equals(teacher)) {
                      jSplitPane.setDividerLocation(150);
                      jSplitPane.setRightComponent(new Teacher());


                  } else if (lastPathComponent.equals(aaClass)) {
                      jSplitPane.setDividerLocation(150);
                      jSplitPane.setRightComponent(new Course());

                  }else if (lastPathComponent.equals(course)){
                      jSplitPane.setDividerLocation(150);
                      jSplitPane.setRightComponent(new CourseApply());
                  }
              }
          });

          //设置tree默认选中学生管理
          tree.setSelectionRow(1);
          jFrame.add(jSplitPane);

    }
}
